package cz.vutbr.fit.xzemko01.tfmcbs.diagram.part;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.tooling.runtime.structure.DiagramStructure;

import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ArtifactEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ArtifactNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ArtifactStereotypeEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponent2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponentCompositeComponentCompartment2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponentCompositeComponentCompartmentEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponentEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponentName2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponentNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponentStereotype2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponentStereotypeEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.DelegateEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.DependencyEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.DependencyNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.DependencyStereotypeEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.GeneralizationEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.GeneralizationNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.Interface2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.InterfaceEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.InterfaceName2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.InterfaceNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ManifestationEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ManifestationNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ModelEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ProvidingPortEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ProvidingPortNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.RealizationEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.RealizationNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.RequiringPortEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.RequiringPortNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.SimpleComponent2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.SimpleComponentEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.SimpleComponentName2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.SimpleComponentNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.SimpleComponentStereotype2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.SimpleComponentStereotypeEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.UsageEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.UsageNameEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.WrappingLabel2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.WrappingLabel3EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.WrappingLabelEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.model.Model;
import cz.vutbr.fit.xzemko01.tfmcbs.model.meta.TfmcbsPackage;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class ComponentBasedSystemVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (ModelEditPart.MODEL_ID.equals(view.getType())) {
				return ModelEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
				.getVisualID(view.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				ComponentBasedSystemDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (TfmcbsPackage.eINSTANCE.getModel().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((Model) domainElement)) {
			return ModelEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
				.getModelID(containerView);
		if (!ModelEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (ModelEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = ModelEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case ModelEditPart.VISUAL_ID:
			if (TfmcbsPackage.eINSTANCE.getSimpleComponent().isSuperTypeOf(
					domainElement.eClass())) {
				return SimpleComponentEditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getCompositeComponent().isSuperTypeOf(
					domainElement.eClass())) {
				return CompositeComponentEditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getArtifact().isSuperTypeOf(
					domainElement.eClass())) {
				return ArtifactEditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getInterface().isSuperTypeOf(
					domainElement.eClass())) {
				return InterfaceEditPart.VISUAL_ID;
			}
			break;
		case CompositeComponentEditPart.VISUAL_ID:
			if (TfmcbsPackage.eINSTANCE.getProvidingPort().isSuperTypeOf(
					domainElement.eClass())) {
				return ProvidingPortEditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getRequiringPort().isSuperTypeOf(
					domainElement.eClass())) {
				return RequiringPortEditPart.VISUAL_ID;
			}
			break;
		case CompositeComponent2EditPart.VISUAL_ID:
			if (TfmcbsPackage.eINSTANCE.getProvidingPort().isSuperTypeOf(
					domainElement.eClass())) {
				return ProvidingPortEditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getRequiringPort().isSuperTypeOf(
					domainElement.eClass())) {
				return RequiringPortEditPart.VISUAL_ID;
			}
			break;
		case CompositeComponentCompositeComponentCompartmentEditPart.VISUAL_ID:
			if (TfmcbsPackage.eINSTANCE.getSimpleComponent().isSuperTypeOf(
					domainElement.eClass())) {
				return SimpleComponent2EditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getCompositeComponent().isSuperTypeOf(
					domainElement.eClass())) {
				return CompositeComponent2EditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getInterface().isSuperTypeOf(
					domainElement.eClass())) {
				return Interface2EditPart.VISUAL_ID;
			}
			break;
		case CompositeComponentCompositeComponentCompartment2EditPart.VISUAL_ID:
			if (TfmcbsPackage.eINSTANCE.getSimpleComponent().isSuperTypeOf(
					domainElement.eClass())) {
				return SimpleComponent2EditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getCompositeComponent().isSuperTypeOf(
					domainElement.eClass())) {
				return CompositeComponent2EditPart.VISUAL_ID;
			}
			if (TfmcbsPackage.eINSTANCE.getInterface().isSuperTypeOf(
					domainElement.eClass())) {
				return Interface2EditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
				.getModelID(containerView);
		if (!ModelEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (ModelEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = ModelEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case ModelEditPart.VISUAL_ID:
			if (SimpleComponentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (CompositeComponentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ArtifactEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (InterfaceEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case SimpleComponentEditPart.VISUAL_ID:
			if (SimpleComponentNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SimpleComponentStereotypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case CompositeComponentEditPart.VISUAL_ID:
			if (CompositeComponentNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (CompositeComponentStereotypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (CompositeComponentCompositeComponentCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ProvidingPortEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (RequiringPortEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ArtifactEditPart.VISUAL_ID:
			if (ArtifactNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ArtifactStereotypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case InterfaceEditPart.VISUAL_ID:
			if (InterfaceNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case SimpleComponent2EditPart.VISUAL_ID:
			if (SimpleComponentName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SimpleComponentStereotype2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case CompositeComponent2EditPart.VISUAL_ID:
			if (CompositeComponentName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (CompositeComponentStereotype2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (CompositeComponentCompositeComponentCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ProvidingPortEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (RequiringPortEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Interface2EditPart.VISUAL_ID:
			if (InterfaceName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ProvidingPortEditPart.VISUAL_ID:
			if (ProvidingPortNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (WrappingLabelEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RequiringPortEditPart.VISUAL_ID:
			if (RequiringPortNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (WrappingLabel2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case CompositeComponentCompositeComponentCompartmentEditPart.VISUAL_ID:
			if (SimpleComponent2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (CompositeComponent2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Interface2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case CompositeComponentCompositeComponentCompartment2EditPart.VISUAL_ID:
			if (SimpleComponent2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (CompositeComponent2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Interface2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DependencyEditPart.VISUAL_ID:
			if (DependencyNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DependencyStereotypeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ManifestationEditPart.VISUAL_ID:
			if (ManifestationNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (WrappingLabel3EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RealizationEditPart.VISUAL_ID:
			if (RealizationNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case UsageEditPart.VISUAL_ID:
			if (UsageNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case GeneralizationEditPart.VISUAL_ID:
			if (GeneralizationNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (TfmcbsPackage.eINSTANCE.getDependency().isSuperTypeOf(
				domainElement.eClass())) {
			return DependencyEditPart.VISUAL_ID;
		}
		if (TfmcbsPackage.eINSTANCE.getManifestation().isSuperTypeOf(
				domainElement.eClass())) {
			return ManifestationEditPart.VISUAL_ID;
		}
		if (TfmcbsPackage.eINSTANCE.getRealization().isSuperTypeOf(
				domainElement.eClass())) {
			return RealizationEditPart.VISUAL_ID;
		}
		if (TfmcbsPackage.eINSTANCE.getUsage().isSuperTypeOf(
				domainElement.eClass())) {
			return UsageEditPart.VISUAL_ID;
		}
		if (TfmcbsPackage.eINSTANCE.getGeneralization().isSuperTypeOf(
				domainElement.eClass())) {
			return GeneralizationEditPart.VISUAL_ID;
		}
		if (TfmcbsPackage.eINSTANCE.getDelegate().isSuperTypeOf(
				domainElement.eClass())) {
			return DelegateEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(Model element) {
		return true;
	}

	/**
	 * @generated
	 */
	public static boolean checkNodeVisualID(View containerView,
			EObject domainElement, int candidate) {
		if (candidate == -1) {
			//unrecognized id is always bad
			return false;
		}
		int basic = getNodeVisualID(containerView, domainElement);
		return basic == candidate;
	}

	/**
	 * @generated
	 */
	public static boolean isCompartmentVisualID(int visualID) {
		switch (visualID) {
		case CompositeComponentCompositeComponentCompartmentEditPart.VISUAL_ID:
		case CompositeComponentCompositeComponentCompartment2EditPart.VISUAL_ID:
			return true;
		default:
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static boolean isSemanticLeafVisualID(int visualID) {
		switch (visualID) {
		case ModelEditPart.VISUAL_ID:
			return false;
		case SimpleComponentEditPart.VISUAL_ID:
		case ArtifactEditPart.VISUAL_ID:
		case InterfaceEditPart.VISUAL_ID:
		case SimpleComponent2EditPart.VISUAL_ID:
		case Interface2EditPart.VISUAL_ID:
		case ProvidingPortEditPart.VISUAL_ID:
		case RequiringPortEditPart.VISUAL_ID:
			return true;
		default:
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static final DiagramStructure TYPED_INSTANCE = new DiagramStructure() {
		/**
		 * @generated
		 */
		@Override
		public int getVisualID(View view) {
			return cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
					.getVisualID(view);
		}

		/**
		 * @generated
		 */
		@Override
		public String getModelID(View view) {
			return cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
					.getModelID(view);
		}

		/**
		 * @generated
		 */
		@Override
		public int getNodeVisualID(View containerView, EObject domainElement) {
			return cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
					.getNodeVisualID(containerView, domainElement);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean checkNodeVisualID(View containerView,
				EObject domainElement, int candidate) {
			return cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
					.checkNodeVisualID(containerView, domainElement, candidate);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean isCompartmentVisualID(int visualID) {
			return cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
					.isCompartmentVisualID(visualID);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean isSemanticLeafVisualID(int visualID) {
			return cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry
					.isSemanticLeafVisualID(visualID);
		}
	};

}
